package com.cqjtu.service.serviceImpl;

import com.cqjtu.mapper.ProjectMapper;
import com.cqjtu.mapper.TeamsMapper;
import com.cqjtu.mapper.UnitMapper;
import com.cqjtu.pojo.PageBean;
import com.cqjtu.pojo.Result;
import com.cqjtu.pojo.Unit;
import com.cqjtu.service.UnitService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UnitServiceImpl implements UnitService {

    @Autowired
    private UnitMapper unitMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private TeamsMapper teamsMapper;
    @Override
    public PageBean page(Integer page, Integer pageSize, String unitName, String unitCategory) {

        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.执行查询表
        List<Unit> unitList = unitMapper.list(unitName,unitCategory);
        Page<Unit> p = (Page<Unit>) unitList;
        //3.封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public void add(Unit unit) {
        unitMapper.insert(unit);
    }

    @Override
    public Unit getById(Integer unitId) {
        return unitMapper.getById(unitId);
    }

    @Override
    public void update(Unit unit) {
        unitMapper.update(unit);
    }

    @Override
    public boolean delete(List<Integer> unitIds) {

        Long pl = projectMapper.selectByUnitIds(unitIds);
        Long tl = teamsMapper.selectByUnitIds(unitIds);
        if(pl != 0 || tl != 0){
            return false;
        }

        unitMapper.delete(unitIds);

        return true;
    }
}
